//
//  LoginCell.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/19.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "LoginCell.h"
@interface LoginCell()<UITextFieldDelegate>
{
    NSMutableArray *titileArr;
    NSMutableArray *imtArr;
    
    int countDown;                  //倒计时
}
@property (strong, nonatomic) IBOutlet UITextField *inputText;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, strong) UIButton *verifyBtn;

@end

@implementation LoginCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    titileArr = [NSMutableArray arrayWithObjects:@"请输入11位手机号码", @"验证码",nil];
    imtArr = [NSMutableArray arrayWithObjects:@"relation", @"verify",nil];
    
    countDown = 60;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)configureCell:(NSInteger)index{
    //左边
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//    leftView.backgroundColor = [UIColor yellowColor];
    
    UIImageView *searchImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 7, 18, 20)];
    searchImg.image = HTCFQuickImage(imtArr[index]);
    [leftView addSubview:searchImg];
    
    _inputText.leftView = leftView;
    _inputText.leftViewMode = UITextFieldViewModeAlways;
    _inputText.tag = 100 + index;
    
    if (index == 0) {
        _inputText.placeholder = titileArr[index];
        
    } else {
        _inputText.placeholder = titileArr[index];
        
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        rightView.backgroundColor = HTCFQuickRGBColor(255, 255, 255);
        rightView.userInteractionEnabled = YES;
        
        _verifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _verifyBtn.frame = CGRectMake(5, 5, 95, 20);
        
        _verifyBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        [_verifyBtn setTitleColor:HTCFQuickRGBColor(51, 51, 51) forState:UIControlStateNormal];
        [_verifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_verifyBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [rightView addSubview:_verifyBtn];
        
        _inputText.rightView = rightView;
        _inputText.rightViewMode = UITextFieldViewModeAlways;
    }
}

- (void)btnClick{
    countDown = 60;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(timerAction)];
    [self.displayLink setFrameInterval:60];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    if ([_delegate respondsToSelector:@selector(transmit)]) {
        [_delegate performSelector:@selector(transmit) withObject:self];
    }
}

- (void)timerAction
{
    if (countDown <= 0) {
        [_verifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verifyBtn.enabled = YES;
        [self stopDisplayLink];
    }else
    {
        NSString *timeStr = [NSString stringWithFormat:@"%ds后重发",countDown];
        [_verifyBtn setTitle:timeStr forState:UIControlStateDisabled];
        _verifyBtn.enabled = NO;
        countDown--;
    }
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

#pragma mark -textFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.contentView endEditing:YES];
}

@end
